package com.mioto.ld.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author mioto-qinxj
 * @date 2024/6/26
 * @desc
 */
@Component
@Slf4j
public class MailHelper {
    private String host;
    private int port;
    private String from;
    private String pass;

    public void send(String subject,String content,String... tos){
        MailAccount account = new MailAccount();
        account.setHost(host);
        account.setPort(port);
        account.setAuth(true);
        account.setFrom(from);
        account.setUser(from);
        account.setPass(pass);
        try {
            MailUtil.send(account,CollUtil.newArrayList(tos),subject,content,false, new File[0]);
            log.info("send mail to - {} success",tos);
        }catch (Exception e){
            log.error("send mail to - {} fail",tos,e);
        }
    }

    @Value("${mail.host}")
    public void setHost(String host) {
        this.host = host;
    }
    @Value("${mail.port}")
    public void setPort(int port) {
        this.port = port;
    }
    @Value("${mail.from}")
    public void setFrom(String from) {
        this.from = from;
    }
    @Value("${mail.pass}")
    public void setPass(String pass) {
        this.pass = pass;
    }
}
